(function () {
    var core = ecui,
        dom = ecui.dom;

    ecui.util.extend(NS.ui, {
        ContainerTab: ecui.inherits(
            ecui.ui.Tab,
            {
                ontitleclick: function (event) {
                    if (event.index === 1) {
                       ecui.dispatchEvent(ecui.get('mine-approved-list-view'), 'refresh');
                    }
                }
            }
        ),
        ListTab: ecui.inherits(
            mao.ui.Tab,
            {
                ontitleclick: function (event) {
                    if (this.getSelected() !== event.item) {
                        if (event.index === 1) {
                            var listView = ecui.query(function (item) { return item instanceof mao.ui.AppListView && dom.contain(this.getItem(1).getContainer(), item.getMain()); }.bind(this));
                            ecui.dispatchEvent(listView[0], 'refresh');
                        }
                    } else {
                        this.setSelected(0);
                        return false;
                    }
                    mao.ui.Tab.prototype.ontitleclick.call(this, event);
                }
            }
        ),
        // 搜索框
        SearchText: core.inherits(
            mao.ui.SearchText,
            function (el, options) {
                this.hContainer = dom.next(el);
                this.listContainer = dom.next(this.hContainer);
                mao.ui.SearchText.call(this, el, options);
                // 添加取消按钮
                // dom.insertHTML(el, 'BEFOREEND', '<div class="ui-search-close">取消</div>');
                core.$fastCreate(this.Close, dom.last(el), this, {});
            },
            {
                Close: ecui.inherits(
                    ecui.ui.Control,
                    {
                        onclick: function (event) {
                            this.findControl(ecui.ui.Tab).setSelected(0);
                        }
                    }
                ),
                oninput: function () {
                    // if (this.getValue()) {
                    //     dom.addClass(this.hContainer, 'ui-hide');
                    // } else {
                    //     dom.removeClass(this.hContainer, 'ui-hide');
                    // }

                    if (!dom.hasClass(this.hContainer, 'ui-hide')) {
                        dom.addClass(this.hContainer, 'ui-hide');
                    }
                    // ecui.esr.callRoute(this.hContainer.route, true);
                    ecui.dispatchEvent(this.listContainer.getControl(), 'refresh');
                },
                onfocus: function () {
                    dom.removeClass(this.hContainer, 'ui-hide');
                    ecui.esr.callRoute(this.hContainer.route, true);
                },
                onkeydown: function (event) {
                    if (event.which === 13) {
                        dom.addClass(this.hContainer, 'ui-hide');
                        // 刷新列表
                        ecui.dispatchEvent(this.listContainer.getControl(), 'refresh');
                        ecui.dispatchEvent(this, 'blur');
                        event.preventDefault();
                        event.stopPropagation();
                    }
                }
            }
        ),
        History: core.inherits(
            ecui.ui.Control,
            function (el, options) {
                ecui.ui.Control.call(this, el, options);
                this._sValue = options.value;
            },
            {
                onclick: function () {
                    var searchContent = ecui.ui.Button.prototype.getForm.call(this).elements.searchContent.getControl(),
                        listContainer = ecui.dom.next(ecui.$(ecui.esr.findRoute(this).main)).getControl();
                    searchContent.setValue(this._sValue);
                    dom.addClass(dom.next(searchContent.getMain()), 'ui-hide');
                    // ecui.esr.callRoute('searchList', true);
                    ecui.dispatchEvent(listContainer, 'refresh');
                }
            }
        ),
        ClearHistory: core.inherits(
            mao.ui.Submit,
            {
                onclick: function () {
                    this.request(
                        'data@GET /erp-management/search/history/delete',
                        function () {
                            var data = ecui.esr.getData('data');
                            if (data instanceof Object) {
                                var order_history_container = ecui.$('order_history_container') || ecui.$('todo_history_container');
                                order_history_container.innerHTML = ecui.esr.getEngine().render('history_list', {});
                                ecui.init(order_history_container);
                            }
                        },
                        function () {}
                    );
                }
            }
        )
    });
    Object.assign(NS.data, {
        routes: ['', '#/approval/detail.order', '#/approval/detail.contract', '#/approval/detail.play', '#/approval/detail.sponsor', '#/approval/detail.musician'],
        status: {
            auditState: {
                value: '',
                title: '状态',
                select: '',
                items: [{
                    value: '',
                    code: '全部'
                }, {
                    value: '1',
                    code: '待审批'
                }, {
                    value: '2',
                    code: '审批完成'
                }, {
                    value: '3',
                    code: '已驳回'
                }, {
                    value: '4',
                    code: '已撤销'
                }, {
                    value: '5',
                    code: '已终止'
                }]
            },
            module: {
                value: '',
                title: '类型',
                select: '',
                items: [{
                    'value': '',
                    'code': '全部'
                }, {
                    'value': '1',
                    'code': '预定场'
                }, {
                    'value': '2',
                    'code': '定场'
                }, {
                    'value': '3',
                    'code': '巡演'
                }, {
                    'value': '4',
                    'code': '包场'
                }, {
                    'value': '5',
                    'code': '分票'
                }, {
                    'value': '6',
                    'code': '商务'
                }, {
                    'value': '7',
                    'code': '自办'
                }, {
                    'value': '8',
                    'code': '音乐人'
                }, {
                    'value': '9',
                    'code': '主办方'
                }, {
                    'value': '10',
                    'code': '演出'
                }]
            }
        }
    });
    Object.assign(NS.data, {
        'sortBarConfigs': [
            { type: 'status', typeName: '筛选', active: false, target: 'status_selected', selects: NS.data.status /*, number: '0'*/ }
        ]
    });
    ecui.esr.addRoute('approve', {
        model: [''],
        weight: 3,
        // queryUrl: '@FORM /erp-management/customer/musician/select-list-page?approveListSearchForm',
        queryUrl: '@FORM /erp-management/data/approval-center?mineApproveFilterListForm&type=1&saveHistory=1',
        queryUrl1: '@FORM /erp-management/data/approval-center?mineApproveFilterListForm&mineApproveSearchListForm&type=1&saveHistory=1',
        queryUrl2: '@FORM /erp-management/data/approval-center?mineApprovedFilterListForm&type=2&saveHistory=1',
        queryUrl3: '@FORM /erp-management/data/approval-center?mineApprovedFilterListForm&mineApprovedSearchListForm&type=2&saveHistory=1',
        // children: 'music_people_list',
        main: 'approval_approve',
        view: 'approveList',
        frame: true,
        children: ['historyList', 'appledHistoryList'],
        onbeforerender: function (context) {
        },
        onafterrender: function (context) {
            // var approveListView = ecui.get('mine-approve-list-view');
            // var approveSearchListView = ecui.get('mine-approve-search-list-view');
            // var approvedListView = ecui.get('mine-approved-list-view');
            // var approvedSearchListView = ecui.get('mine-approved-search-list-view');

            // approveSearchListView.setUrlName('queryUrl1');
            // approvedListView.setUrlName('queryUrl2');
            // approvedSearchListView.setUrlName('queryUrl3');

            // ecui.dispatchEvent(approveListView, 'refresh');
            // approveListView.onrequest = function (event) {
            //     var title = this.getParent().getParent().getParent().getBody();
            //     dom.first(title).innerHTML = '待我审批的(' + event.data.total + ')';
            // };
        }
    });
    // 待审批的搜索历史
    core.esr.addRoute('historyList', {
        model: [
            'historys@GET /erp-management/search/history/list'
        ],
        main: 'order_apply_history_container',
        view: 'history_list',
        frame: true,
        weight: 3,
        onbeforerender: function (context) {
            // console.log(context);
        },
        onafterrender: function () {
        },
        oncached: function () {
        }
    });
    // 已审批的搜索历史
    core.esr.addRoute('appledHistoryList', {
        model: [
            'historys@GET /erp-management/search/history/list'
        ],
        main: 'order_appled_history_container',
        view: 'history_list',
        frame: true,
        weight: 3,
        onbeforerender: function (context) {
            // console.log(context);
        },
        onafterrender: function () {
        },
        oncached: function () {
        }
    });
}());